package com.onlineauction.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.onlineauction.entities.Auction;
import com.onlineauction.entities.User;
import com.onlineauction.exceptions.KeyNameEmptyException;
import com.onlineauction.exceptions.UsernameAlreadyExistsException;
import com.onlineauction.helpers.Constants;
import com.onlineauction.helpers.Constants.Usertype;

public class RegisterAccountServlet extends HttpServlet {

	private static final long serialVersionUID = 2276053314749945364L;

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
			ServletException {
		String username = request.getParameter(Constants.USERNAME);
		String password = request.getParameter(Constants.PASSWORD);
		String passwordConfirm = request.getParameter(Constants.PASSWORD_CONFIRM);
		Usertype usertype = null;
		try {
			usertype = Usertype.valueOf(request.getParameter(Constants.USERTYPE));
		} catch (Exception e) {
			usertype = null;
		}
		System.out.println(usertype);

		if (username == null || password == null || usertype == null) {
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		}
		
		if (username == null || password == null || usertype == null || username.length() == 0
				|| password.length() == 0) {			
			String message = "Username, password and user type cannot be empty.";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		}

		if (!password.equals(passwordConfirm)) {
			String message = "Passwords entered are not same.";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		}

		if (!User.isUsernameAvailable(username)) {
			String message = "Username is already used, please try another one.";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		}

		User user = null;
		try {
			user = new User(username, password, usertype, true);
		} catch (KeyNameEmptyException e) {
			String message = "Username cannot be empty.";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		} catch (UsernameAlreadyExistsException e) {
			String message = "Username already exists, please try another one.";
			request.setAttribute(Constants.MESSAGE, message);
			request.getRequestDispatcher("/register.jsp").forward(request, response);
			return;
		}

		Auction.markExpiredAuctions();
		username = user.getUsername();
		request.getSession(true).setAttribute(Constants.USERNAME, username);
		response.sendRedirect("/Home");
		return;
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		doGet(request, response);
	}

}
